package myapp.services;

import java.util.ArrayList;

import myapp.DAO.MyXmlParser;
import myapp.DTO.CategoryDTO;
import myapp.DTO.ServiceDTO;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;


public class servicesHelper {
	//private static String SOAP_ACTION = "http://tay.com/getCategory";
	//public  final String OPERATION_NAME = "getCategory"; 
	public  final String WSDL_TARGET_NAMESPACE = "http://tay.com/";
	public  final String SOAP_ADDRESS = "http://taynguyen144.somee.com/DbService.asmx";

	private String callServicegetAllCategories()
	{
		String OPERATION_NAME = "getAllCategory";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;

		envelope.setOutputSoapObject(request);

		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
		Object response=null;
		try
		{
			//httpTransport.call("http://tay.com/getAllCategory", envelope);
			httpTransport.call(WSDL_TARGET_NAMESPACE + OPERATION_NAME, envelope);
			response = envelope.getResponse();
		}
		catch (Exception exception)
		{
			response=exception.toString();
		}
		if (response == null)
			return null;

		return response.toString();
	}
	
	private String callServiceGetAllServiceDTO()
	{
		String OPERATION_NAME = "getAllServices";

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;

		envelope.setOutputSoapObject(request);

		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
		Object response=null;
		try
		{
			//httpTransport.call("http://tay.com/getAllCategory", envelope);
			httpTransport.call(WSDL_TARGET_NAMESPACE + OPERATION_NAME, envelope);
			response = envelope.getResponse();
		}
		catch (Exception exception)
		{
			response=exception.toString();
		}
		if (response == null)
			return null;

		return response.toString();
	}

	public ArrayList<CategoryDTO> getAllCategories()
	{
		String src = callServicegetAllCategories();		
		return MyXmlParser.toCategoryDTOs(src);
	}

	public ArrayList<ServiceDTO> getAllServices()
	{
		String src = callServiceGetAllServiceDTO();
		return MyXmlParser.toServiceDTOs(src);
	}

	private String callServiceGetCategory(int id){
		String OPERATION_NAME = "getCategory";
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
		PropertyInfo pi=new PropertyInfo();
		pi.setName("_id");
		pi.setValue(id);
		pi.setType(Integer.class);
		request.addProperty(pi);
		//pi=new PropertyInfo();
		//pi.setName("b");
		//pi.setValue(b);
		//pi.setType(Integer.class);
		//request.addProperty(pi);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = true;

		envelope.setOutputSoapObject(request);

		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
		Object response=null;
		try
		{
			httpTransport.call(WSDL_TARGET_NAMESPACE + OPERATION_NAME, envelope);
			response = envelope.getResponse();
		}
		catch (Exception exception)
		{
			response=exception.toString();
		}

		if (response == null)
			return null;

		return response.toString();
	}


	private String callGetServicesInDistance(double yourLat, double yourLong, double distance){
		String OPERATION_NAME = "getServiceInDistance";
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);

		request.addProperty("yourLat", yourLat);
		request.addProperty("yourLong", yourLong);
		request.addProperty("distance", distance);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
		envelope.dotNet = true;

		envelope.setOutputSoapObject(request);
		//Register to process with double type
		MarshalDouble md = new MarshalDouble();
		md.register(envelope);

		HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
		Object response=null;
		try
		{
			httpTransport.call(WSDL_TARGET_NAMESPACE + OPERATION_NAME, envelope);
			response = envelope.getResponse();
		}
		catch (Exception exception)
		{
			response=exception.toString();
		}

		if (response == null)
			return null;

		return response.toString();
	}

	public ArrayList<ServiceDTO> getServicesInDistance(double yourLat, double yourLong, int distance){
		String wsResult = callGetServicesInDistance(yourLat, yourLong, distance);
		return MyXmlParser.toServiceDTOs(wsResult);
	}
}
